package com.erp.controller.financeController;

import com.erp.pojo.Income;
import com.erp.service.financeService.IncomeService;
import com.erp.utils.Msg;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;
import java.util.Date;


/**
 * Description: TeamProject
 * Created by ${ZhangZhenJun}on 2019/1/10.
 */
@RestController
@RequestMapping("/income")
public class IncomeController {

    @Autowired
    private IncomeService incomeService;

    /**
     * 查询所有信息+模糊查询
     */
    @PostMapping("/selectAll")
    public Msg selectAll(@RequestParam(value = "page",defaultValue = "1")int pageNum,
                         @RequestParam(value = "limit",defaultValue = "10")int pageSize,
                         @RequestParam(value = "billNo",defaultValue = "null") String billNo,
                         @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") Date billTimeBefore,
                         @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") Date billTimeBack,
                         @RequestParam("type")String type){
        System.out.println("进入查询控制器");
        System.out.println(billNo+"-"+billTimeBefore+"-"+billTimeBack+"-"+type);
        Msg msg = incomeService.selectIncomeService(pageNum, pageSize, billNo, billTimeBefore, billTimeBack,type);
        System.out.println("==================="+msg+"============================");
        return msg;
    }

    @PostMapping("/insert")
    public String insert(Income income){

        System.out.println("插入控制器"+income);

        incomeService.insertIncomeInoutitem(income);
        incomeService.insertIncomeAccount(income);
        incomeService.insertIncomePerson(income);
        incomeService.insertIncomeSupplier(income);
        incomeService.insertIncomeAccounthead(income);
        incomeService.insertIncomeAccountitem(income);
        return "成功";
    }

    @DeleteMapping("/delete/{accountId}")
    public void delete(@PathVariable("accountId")Long accountId){
        incomeService.deleteIncome(accountId);
    }

    @DeleteMapping("/deleteAll/{accountId}")
    public void deleteAll(@PathVariable("accountId")String accountId){
        /*将id转换成String类型，并去掉逗号*/
        String d[] = accountId.split(",");
        /*System.out.println(Arrays.toString(d));*/
        for (int i = 0; i < d.length; i++) {
            Long id = Long.valueOf(d[i]);
            incomeService.deleteIncome(id);
        }
    }

}
